package com.ayuku.course.ui.coursepackage;

import java.math.BigDecimal;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.SqliteDB;
import com.ayuku.commons.Util;
import com.ayuku.commons.ui.ListWithFormMaster;
import com.ayuku.course.persistence.model.CoursePackage;
import com.ayuku.course.util.UtilCourse;

public class ListCoursePackage extends ListWithFormMaster {
	private CompCoursePackage comp;
	private CoursePackage pjo;
	private String name;
	private String kode;
	private String categoryCode;
	private BigDecimal fee;
	private String levelCode;

	public ListCoursePackage(Composite parent, int style) {
		super(parent, style);
	}

	@Override
	protected String[] getColumCaption() {
		final String[] titles = { "Kode", "Nama", "Kategory", "Level", "Biaya" };
		return titles;
	}

	@Override
	protected int[] getColumWidth() {
		int[] i = { 80, 200, 100, 100, 120 };
		return i;
	}

	@Override
	protected void setFormKeyWithSelectedTableItem() {
		TableItem[] selection = getTableData().getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			setFormKey(itm.getText(0));
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN, getShell());
		}
	}

	@Override
	public void additionalProcess() {

		System.out.println("... masuk additinal proses ...");
		try {
			UtilCourse.setCmbCourseLevel(comp.getCmbLevel());
			UtilCourse.setCmbCategory(comp.getCmbCategory());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static List<CoursePackage> getList(String arg) throws Exception {
		return SqliteDB
				.getPersist()
				.readList(
						CoursePackage.class,
						"SELECT * FROM COURSE_PACKAGE WHERE code LIKE ? or name LIKE ? limit 10000",
						new Object[] { arg, arg });
	}

	@Override
	protected void populateTable(String sqlDml) throws Exception {
		getTableData().removeAll();
		List<CoursePackage> lst = getList(sqlDml);
		getTableData().setItemCount(lst.size());
		setData(new Object[lst.size()][]);

		for (int i = 0; i < lst.size(); i++) {
			CoursePackage pj = lst.get(i);
			getData()[i] = new String[] { Util.nullToString(pj.getCode()),
					Util.nullToString(pj.getName()), pj.getCategoryCode(),
					pj.getLevelCode(), Util.formatToNumber(pj.getFee()) };
		}
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
		item.setText(4, Util.nullToString(obj[4]));
	}

	@Override
	public void getCompositeForm(Composite formContainer) {
		comp = new CompCoursePackage(formContainer, SWT.BORDER);

	}

	@Override
	public void reloadForm() {

		if (pjo != null) {
			comp.getTxtCode().setText(Util.nullToString(pjo.getCode()));
			comp.getTxtName().setText(Util.nullToString(pjo.getName()));
			comp.getTxtBiaya().setValue(Util.nullToBigDecimal(pjo.getFee()));
			comp.getCmbCategory().setText(Util.nullToString(pjo.getCategoryCode()));
			comp.getCmbLevel().setText(Util.nullToString(pjo.getLevelCode()));
		}
	}

	@Override
	public void doClearForm(SelectionEvent arg0) {
		pjo = new CoursePackage();
		reloadForm();
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void enabledControl(boolean otherfield, boolean key) {
		comp.getTxtCode().setEditable(key);
		comp.getTxtName().setEditable(otherfield);

		// comp.getTxtKode().setText("");
		// comp.getTxtNama().setText("");
	}

	@Override
	public boolean doValidate() {

		kode = comp.getTxtCode().getText();
		if (Util.isEmpty(kode)) {
			Util.showMsgBox("Kode harus dilengkapi!", 1, getShell());
			comp.getTxtCode().setFocus();
			return false;
		}

		name = comp.getTxtName().getText();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getShell());
			comp.getTxtName().setFocus();
			return false;
		}

		categoryCode = comp.getCmbCategory().getText();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getShell());
			comp.getTxtName().setFocus();
			return false;
		}
		
		fee = comp.getTxtBiaya().getValue();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getShell());
			comp.getTxtName().setFocus();
			return false;
		}
		
		levelCode = comp.getCmbLevel().getText();
		if (Util.isEmpty(levelCode)) {
			Util.showMsgBox("Level harus dilengkapi!", 1, getShell());
			comp.getCmbLevel().setFocus();
			return false;
		}
		return true;
	}

	@Override
	protected void setPojoWithFormField() throws Exception {
		if (pjo == null) {
			pjo = new CoursePackage();
		}
		pjo.setCode(kode);
		pjo.setName(name);
		pjo.setCategoryCode(categoryCode);
		pjo.setFee(fee);
		pjo.setLevelCode(levelCode);
	}

	@Override
	protected void getPojoByFormKey(String key) throws Exception {
		try {
			pjo = (CoursePackage) SqliteDB.getPersist().readByPrimaryKey(
					CoursePackage.class, new Object[] { getFormKey() });
		} catch (Exception e) {

		}
		reloadForm();
	}

	@Override
	protected int doMainInsert() throws Exception {
		setPojoWithFormField();
		return SqliteDB.getPersist().insert(pjo);
	}

	@Override
	protected int doMainUpdate() throws Exception {
		setPojoWithFormField();
		return SqliteDB.getPersist().update(pjo);
	}

	@Override
	protected int doMainDelete() throws Exception {
		return delete(pjo.getCode());
	}

	@Override
	protected int delete(String key) throws Exception {
		return SqliteDB.getPersist().executeUpdate(
				"delete from course_package where code = ?",
				new Object[] { key.trim() });
	}

	@Override
	public void setFocusForAdd() {
		comp.getTxtCode().setFocus();
	}

	@Override
	public void setFocusForEdit() {
		comp.getTxtName().setFocus();
	}

}
